Grokking Algorithm Complexity and Big-O
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Course Overview
Basics of Algorithm Analysis
Introduction to Algorithm Analysis
Measuring Efficiency
Functions and Their Growth Rates
Asymptotic Notations
Overview of Asymptotic Analysis
Big-O Notation (O-notation)
Big-Omega Notation (Ω-notation)
Big-Theta Notation (Θ-notation)
Little-o and Little-omega Notations
Comparing Asymptotic Notations
Time Complexity Analysis
Understanding Time Complexity
Analyzing Control Structures
Analyzing Simple Algorithms
Best, Worst, and Average Cases
Constant Time: O(1)
Linear Time: O(n)
Quadratic Time: O(n²)
Quiz
Space Complexity Analysis
Understanding Space Complexity
Analyzing Space Complexity of Algorithms
Constant Space: O(1)
Linear Space: O(n)
Quadratic Space: O(n²)
Quiz
Analyzing Recursive Algorithms
Introduction to Recursion
Recursion Tree Method
Recurrence Relation Method
Master Theorem Method
Space Complexity Analysis of Recursive Algorithm
Logarithmic Time and Space: O(log n)
Linearithmic Time: O(n log n)
Exponential Time and Space: O(2ⁿ)
Quiz
Array
Linked List
Hash Table and Set
Stack and Queue
Binary Search Tree
Quiz
Sorting Algorithms
Graph Algorithms
Dynamic Programming Algorithms
Coding Interview Problems
Trade-offs in Algorithm Design
Recursion Tree Method

The Recursion Tree Method is a visual approach to analyzing the time complexity of recursive algorithms. By building a "tree" that shows how the algorithm breaks down the problem with each recursive call, we can observe how many calls occur, how much work is done at each level, and ultimately determine the overall time complexity.

This method provides a step-by-step way to see how recursive calls multiply and the impact of each level on the algorithm’s efficiency.

Key Points of the Recursion Tree Method

  • Breakdown of Recursive Calls: Visualizes each recursive call as a node in a tree, with child nodes representing further recursive calls.
  • Summing Work at Each Level: Each level of the tree represents the work done by recursive calls at that depth, helping us calculate the overall work.
  • Finding Total Complexity: By analyzing the height of the tree and the work done at each level, we can add up the contributions from all levels to find the total time complexity.

Let’s go through this process with the binary search algorithm as an example.

Binary Search Example with Recursion Tree

Binary search is an efficient algorithm for finding a target value in a sorted array. It works by dividing the search range in half with each recursive call. Let’s look at the code for binary search in Python before we analyze its complexity.

Binary Search Code

Java
Saved to Cloud

. . . .
  • Base Case: The function stops when low > high, meaning there’s no range left to search.
  • Recursive Case: The function halves the search range by adjusting low or high, depending on whether the target is greater or less than the middle element.

To analyze the time complexity, let’s visualize the recursion tree:

  1. Initial Call: The root of the tree represents the initial call with the full range [low, high].
  2. Each Level: Each recursive call halves the range, creating a new "level" in the tree with smaller subproblems.
  3. Height of the Tree: Since each level halves the problem size, the height of the tree is , where is the size of the input array.

Here’s how the recursion tree might look for a list of size 8 (n = 8):

Image

Calculating the Time Complexity of Binary Search Using the Recursion Tree

When analyzing binary search, each recursive call reduces the size of the search range by half. This process continues until the range is reduced to a single element, which either matches the target or results in an empty range (base case).

Let’s findout the time complexity of the binary search algorithm.

1. Work per Level

  • At each level of the recursion tree, we perform a constant amount of work. Specifically, we:
    • Calculate the middle index, .
    • Compare the middle element to the target, .
    • Decide which half to search in the next recursive call, .
Image
  • Result: Each level of the tree has a constant amount of work, denoted as .

2. Number of Levels (Height of the Tree)

The height of the recursion tree represents the number of times we can keep halving the search range before reaching a single element (or the base case).

  • Initial Range: elements.
  • First Level: Halves the range to .
  • Second Level: Halves it again to .
  • ... and so on: Each level halves the range until it reaches 1.
Image

Mathematically, we’re looking for the number of times we can divide by 2 until we reach 1. This is represented by:

Solving for , we find:

So, the height of the recursion tree is .

3. Total Time Complexity

Now that we know:

  • Each level requires work.
  • The tree has levels.

The total time complexity is the product of the work per level and the number of levels:

Total Complexity = Time complexity at each level x Total number of levels

Total Complexity =

Introduction to Recursion
Recurrence Relation Method
Mark as Completed

On this page

Key Points of the Recursion Tree Method

Binary Search Example with Recursion Tree

Binary Search Code

Visualizing the Recursion Tree for Binary Search

Calculating the Time Complexity of Binary Search Using the Recursion Tree